package com.gientech.spring;

import com.gientech.spring.model.Coffee;
import com.gientech.spring.service.CoffeeOrderService;
import com.gientech.spring.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * @author Administrator
 */
@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.gientech.spring.mapper")
public class SpringBucksApplication implements ApplicationRunner {

	@Autowired
	private CoffeeService coffeeService;
	@Autowired
	private CoffeeOrderService orderService;

	@Autowired
	private RestTemplate restTemplate;

	public static void main(String[] args) {
		log.info("The Coffee openning.");
//		SpringApplication.run(SpringBucksApplication.class, args);
		new SpringApplicationBuilder().sources(SpringBucksApplication.class).bannerMode(Banner.Mode.OFF).web(WebApplicationType.NONE).run(args);
		log.info("The Coffee close.");
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		Coffee coffee = new Coffee();
		coffee.setName("埃塞俄比亚花魁手冲");
		double amount = 76.12;
		coffee.setPrice(Money.of(CurrencyUnit.of("CNY"), 9600 / 100.0));
		int count = coffeeService.saveCoffee(coffee);
		log.warn("save a new Coffee: {}", count);
		log.info("************************************************");

		List<Coffee> list = coffeeService.findAll();
		log.warn("All Coffee: {}", list);
		log.info("************************************************");

		Coffee temp = coffeeService.findById(6L);
		log.warn("query Coffee by  id = {}, coffee={}", temp.getId(), temp);
		log.info("************************************************");

		temp.setPrice(Money.of(CurrencyUnit.of("CNY"), 99900 / 100.0));
		int updateCnt = coffeeService.update(temp);
		log.warn("update Coffee by  id = {},updateCnt=", temp.getId(), updateCnt);
		Coffee updateTemp = coffeeService.findById(6L);
		log.warn("query Coffee by  id = {}, coffee={}", updateTemp.getId(), updateTemp);
		log.info("************************************************");

/*		log.info("All Coffee: {}", coffeeMapper.findAllWithParam(1,10));

		Optional<Coffee> latte = coffeeService.findOneCoffee("Latte");
		if (latte.isPresent()) {
			CoffeeOrder order = orderService.createOrder("Li Lei", latte.get());
			log.info("Update INIT to PAID: {}", orderService.updateState(order, OrderState.PAID));
			log.info("Update PAID to INIT: {}", orderService.updateState(order, OrderState.INIT));
		}*/
		String coffeeUri = "http://localhost:8080/coffee/";

		ParameterizedTypeReference<List<Coffee>> ptr =
				new ParameterizedTypeReference<List<Coffee>>() {};
		ResponseEntity<List<Coffee>> list1 = restTemplate
				.exchange(coffeeUri, HttpMethod.GET, null, ptr);
		list1.getBody().forEach(c -> log.info("Coffee : {}", c));
	}
}

